// packageJ/phoneBillRecharge/home/home.js
const app = getApp();
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
Page({
  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    mobile: "",
    Editid: "",
    detailShow: false,
    editshow: false,
    detailInfo: [],
    list: [],
    total_page: 0,
    isLoadMore: true,
    recharge_url: "",
    member: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      page: 1,
      list: [],
      total_page: 0,
      isLoadMore: true,
      detailShow: false,
      editshow: false,
      Editid: ''
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.setNavigationBarTitle({
      title: '话费充值',
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },
  getMoreData(){
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let urlStr = app.getNetAddresss("plugin.phone-bill-recharge.frontend.member-phone.index");
      app._postNetWork({
        url: urlStr,
        data: {
          page:this.data.page
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.records.data),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toUrl(e) {
    let {
      id,
      key
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/packageJ/phoneBillRecharge/record/record?id=${id}&ind=${key}`,
    })
  },
  toIndex(e) {
    let {
      id,
      key
    } = e.currentTarget.dataset;
    if (id === 0) {
      wx.navigateTo({
        url: `/packageJ/phoneBillRecharge/index?status=${key}`,
      })
    } else {
      wx.navigateTo({
        url: `/packageJ/phoneBillRecharge/index?status=${key}&rid=${id}`,
      })
    }
  },
  detailShowTap(e) {
    let {
      product_id
    } = e.currentTarget.dataset.item.card_package;
    let {
      mobile
    } = e.currentTarget.dataset.item;

    let urlStr = app.getNetAddresss("plugin.phone-bill-recharge.frontend.member-phone.package-detail");
    app._getNetWork({
      url: urlStr,
      data: {
        product_id: product_id,
        mobile: mobile
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            detailShow: true,
            detailInfo: res.data
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res);
      },
    });
  },
  onClose() {
    this.setData({
      detailShow: false
    })
  },
  phone_newTap() {
    this.setData({
      show: true
    })
  },
  onClickLeft() {
    this.setData({
      show: false,
      editshow: false,
      mobile: '',
      Editid: ''
    })
  },
  rechargeBtn() {
    wx.navigateTo({
      url: '/packageJ/phoneBillRecharge/recharge_phone_bill/recharge_phone_bill'
    })
  },
  deletBtn(e) {
    console.log(e);
    let {
      mobile
    } = e.currentTarget.dataset.item
    let urlStr = app.getNetAddresss("plugin.phone-bill-recharge.frontend.member-phone.unbind");
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: mobile,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg);
          this.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res);
      },
    });
  },
  onClickRight_new() {
    if (!this.data.mobile) {
      app.tips('请填写新增的手机号码')
      return
    }
    let urlStr = app.getNetAddresss("plugin.phone-bill-recharge.frontend.member-phone.bind");
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: this.data.mobile,
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg)
          this.setData({
            show: false
          })
          setTimeout(() => {
            this.getData();
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res);
      },
    });
  },
  onClickRight() {
    if (!this.data.mobile) {
      app.tips('请填写编辑的手机号码')
      return
    }
    let urlStr = app.getNetAddresss("plugin.phone-bill-recharge.frontend.member-phone.update");
    app._getNetWork({
      url: urlStr,
      data: {
        mobile: this.data.mobile,
        id: this.data.Editid
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          app.tips(res.msg)
          this.setData({
            editshow: false,
            mobile: '',
            Editid: ''
          })
          setTimeout(() => {
            this.getData();
          }, 1000);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res);
      },
    });
  },
  taoEditPhone(e) {
    let {
      mobile,
      id
    } = e.currentTarget.dataset.item;
    this.setData({
      mobile,
      Editid: id,
      editshow: true
    })
  },
  getData() {
    wx.showLoading({
      title: "加载中",
    });
    let urlStr = app.getNetAddresss("plugin.phone-bill-recharge.frontend.member-phone.index");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            isLoadMore: true,
            list: res.data.records.data,
            total_page: res.data.records.last_page,
            recharge_url: res.data.recharge_url,
            member: res.data.member
          })
          if (!this.data.total_page) {
            this.setData({
              total_page: false
            })
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: (res) => {
        wx.hideLoading();
        console.log(res);
      },
    });
  },
})